package com.daffodil.workable.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.daffodil.common.controller.ReactiveBaseController;
import com.daffodil.core.annotation.DictConver;
import com.daffodil.core.entity.JsonResult;
import com.daffodil.core.entity.Page;
import com.daffodil.core.entity.TableResult;
import com.daffodil.framework.annotation.AuthPermission;
import com.daffodil.framework.annotation.Log;
import com.daffodil.framework.annotation.Log.BusinessType;
import com.daffodil.util.StringUtils;
import com.daffodil.workable.constant.WorkableConstant;
import com.daffodil.workable.entity.WorkableResult;
import com.daffodil.workable.service.IWorkableResultService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;
import reactor.core.publisher.Mono;

/**
 * -节点执行结果控制层
 * @author yweijain
 * @date 2022-04-12
 * @version 1.0
 * @description
 */
@Api(value = "节点执行结果管理", tags = "节点执行结果管理")
@RestController
@RequestMapping(WorkableConstant.API_CONTENT_PATH + "/result")
public class WorkableResultController extends ReactiveBaseController {

    @Autowired
    private IWorkableResultService workableResultService;

    @ApiOperation("分页查询节点执行结果列表")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @SuppressWarnings("unchecked")
    @DictConver
    @AuthPermission("workable:result:list")
    @GetMapping("/list")
    public Mono<TableResult> list(WorkableResult workableResult, Page page, @ApiIgnore ServerHttpRequest request) {
        initQuery(workableResult, page, request);
        List<WorkableResult> list = workableResultService.selectWorkableResultList(query);
        return Mono.just(TableResult.success(list, query));
    }

    @ApiOperation("获取节点执行结果详情")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @GetMapping("/info")
    public Mono<JsonResult> info(@ApiParam(value = "节点执行结果ID") String id, 
            @ApiParam(value = "计划任务ID") String taskId, 
            @ApiParam(value = "节点编号ID") String nodeId, 
            @ApiParam(value = "追踪标识ID") String traceId,
            @ApiIgnore ServerHttpRequest request) {
        if(StringUtils.isNotEmpty(id)) {
            WorkableResult workableResult = workableResultService.selectWorkableResultById(id);
            return Mono.just(JsonResult.success(workableResult));
        }
        WorkableResult result = new WorkableResult();
        result.setTaskId(taskId);
        result.setNodeId(nodeId);
        result.setTraceId(traceId);
        WorkableResult workableResult = workableResultService.selectWorkableResult(result);
        return Mono.just(JsonResult.success(workableResult));
    }

    @ApiOperation("删除节点执行结果")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @AuthPermission("workable:result:remove")
    @Log(title = "节点执行结果管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public Mono<JsonResult> remove(@RequestBody String[] ids, @ApiIgnore ServerHttpRequest request) {
        workableResultService.deleteWorkableResultByIds(ids);
        return Mono.just(JsonResult.success());
    }

}
